package cn.itcast.bos.service.take_delivery.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.take_delivery.PromotionRepository;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.take_delivery.PromotionService;

@Service
@Transactional//开启事务管理
public class PromotionServiceImpl implements PromotionService {
	@Autowired
	private PromotionRepository promotionRepository;
	
	@Override
	public void save(Promotion promotion) {
		
		promotionRepository.save(promotion);
	}
	
	//查询 活动信息
	@Override
	public Page<Promotion> findAll(Pageable pageable) {
		Page<Promotion> result = promotionRepository.findAll(pageable);
		return result;
	}

	//分页查询  活动促销的  服务实现类方法
	@Override
	public PageBean<Promotion> findPageData(int page, int rows) {
		//构造分页查询参数
		Pageable pageable = new PageRequest(page-1, rows);
		//调用dao层查询数据
		Page<Promotion> pageData = promotionRepository.findAll(pageable);
		
		//将查询到的数据  分装到 pageBean中:总信息条数   每页数据
		PageBean<Promotion> pageBean = new PageBean<Promotion>();
		pageBean.setTotalCount(pageData.getTotalElements());
		pageBean.setPageData(pageData.getContent());
		
		return pageBean;
	}

}
